<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


<form:form id="BlogForm" modelAttribute="blog" method="post" cssClass="inline">
    <fieldset>
		<legend>Edit Blog</legend>
		<spring:hasBindErrors name="blog">
			<div class="error">
				<spring:bind path="blog.*">
					<c:forEach items="${status.errorMessages}" var="error">
						<span><c:out value="${error}"/></span><br>
					</c:forEach>
				</spring:bind>
			</div>
		</spring:hasBindErrors>
		<form:hidden id="id" path="id"/>
		<div class="span-17">
			<label for="title">title:</label>
			<form:input class="span-11" id="title" path="title"/><br/>
		</div>
		<div class="span-17">
			<label for="title">keywords:</label>
			<form:input class="span-11" id="keywords" path="keywords"/><br/>
		</div>
		<div class="span-8">
			<label for="regionId">region:</label>
			<form:select id="regionId" path="regionId">
				<form:option value="" label="Please Select"/>
				<form:options items="${regionList}" itemValue="id" itemLabel="name"/>   
			</form:select>
		</div>
		<div class="span-8">
			<label for="catalog">catalog:</label>
			<form:select id="catalog" path="catalog.id">
				<form:option value="" label="Please Select"/>
				<form:options items="${catalogList}" itemValue="id" itemLabel="name"/>   
			</form:select>
		</div>
		<div class="span-8">
			<label for="type">type:</label>
			<form:radiobutton path="type" value="ORIGINAL"/>ORIGINAL
			<form:radiobutton path="type" value="POSTED"/>POSTED
		</div>
		<div class="span-8">
			<label for="asTop">asTop:</label>
			<form:radiobutton path="asTop" value="true"/>True
			<form:radiobutton path="asTop" value="false"/>false
		</div>
		<div class="span-8">
			<label for="allowComment">allowComment:</label>
			<form:radiobutton path="allowComment" value="true"/>True
			<form:radiobutton path="allowComment" value="false"/>false
		</div>
		<div class="span-17">		
			<form:textarea id="content" path="content" style="width:670px;height:200px;"/><br/>
		</div>
		<div class="span-9">
			<c:choose>
				<c:when test="${blog.asDraft}">
					<a href="javascript:save_to_draft()" id='btn_save_draft' class='rndbutton'><span>Save As Draft</span></a>
					<a href="javascript:save_to_blog()" id='btn_save_blog' class='rndbutton'><span>Post Blog</span></a>
				</c:when>
				<c:otherwise>
					<a href="javascript:save_to_blog()" id='btn_save_blog' class='rndbutton'><span>Update Blog</span></a>
				</c:otherwise>
			</c:choose>
		</div>
	</fieldset>
</form:form>
<script type='text/javascript'>
<!--
	var editor;
	$(document).ready(function(){
		editor = KindEditor.create('textarea[name="content"]',{
			id : 'content',
			resizeMode : 1,
			shadowMode : false,
			allowPreviewEmoticons : false,
			allowUpload : true,
			syncType : 'form',
			urlType : 'domain',
			cssPath : '<c:url value="/resources/js/plugins/code/prettify.css" />',
			uploadJson : '<c:url value="/image/upload" />',
			items : ['bold', 'italic', 'underline', 'strikethrough', 'removeformat','|',
'formatblock', 'fontname', 'fontsize','|',
'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent','|',
'link', 'unlink', 'emoticons', 'image', 'flash', '|',
'selectall','source' ,'about']
		});
	});
//-->
</script>
<script type='text/javascript'>
<!--
	var is_blog = true;
	
	$(document).ready(function(){
		$("#BlogForm").ajaxStart(function(){
			if(is_blog)
				$('#btn_save_blog span').html('posting...');
			else
				$('#btn_save_draft span').html('posting...');
	    });
		$("#BlogForm").ajaxComplete(function(event,request, settings){
			if(is_blog)
				$('#btn_save_blog span').html('Post');
			else
				$('#btn_save_draft span').html('Draft');
	    }); 
	});

	function save_to_draft(){
		is_blog = false;
		editor.sync();
		var form = $('#BlogForm').serialize();
		$.post('<c:url value="/blogs/persistdraft"/>',form,function(data){
			if(data.error == 1)
	    		alert(data.messages);
			else {
				if(confirm("move to draft list?")){
					location.href = '<c:url value="/users/"/>'+data.userId+'/blogs/'+data.blogId;
				}
			}
		});
	}
	
	function save_to_blog(){
		is_blog = true;
		editor.sync();
		var form = $('#BlogForm').serialize();
		$.post('<c:url value="/blogs/persistpost"/>',form,function(data){
			if(data.error == 1)
	    		alert(data.messages);
			else {
				location.href = '<c:url value="/users/"/>'+data.userId+'/blogs/'+data.blogId;
			}
		});
	}
//-->
</script>
